home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Essentials / C++ A'Link Files / 1990 / Aug 90 / CPlus.Dev$ 8⁄24⁄90 / 0178-C++ enum translation-Aug90 < prev    next >
Encoding:
Text File  |  1991-03-06  |  1.3 KB  |  49 lines  |  [TEXT/GEOL]

  1. Item    9958881                         21-Aug-90        19:16PDT
  2.  
  3. From:   D3431                           MacroMind Programmers,PRT
  4.  
  5. To:     CPLUS.DEV$                      C++ Interest List--Developers
  6.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  7.  
  8. Sub:    C++ enum translation riddle
  9.  
  10. Can anyone answer this code generation riddle?
  11.  
  12. Given this in C++:
  13.  
  14.     typedef enum {
  15.           apple = 35, orange, banana
  16.     } fruit;
  17.  
  18.     void func()
  19.     {
  20.           fruit a = apple;
  21.     }
  22.  
  23. Why C code generated is [equivalent to] this?
  24.  
  25.     enum fruit{apple, orange, banana};
  26.  
  27.     typedef unsigned char fruit;
  28.  
  29.     void func()
  30.     {
  31.         unsigned char a = ((unsigned char )35);
  32.     }
  33.  
  34.  
  35. An incorrect enum for fruit is generated (note apple == 0), but that's OK,
  36. since it never passes the enums through to the C compiler!  A type is generated
  37. for fruit, but it is never used?
  38.  
  39. I could understand if all mention of enums vanished, and CFront preprocessed
  40. them into ints (making them conform to C++ and Ansi enums).  I could understand
  41. if enums were simply passed through to the C compiler (allowing enums to be
  42. size optimized as they by MPW C).  I just don't understand the explicit
  43. optimization in output plus spurius (and incorrect) type declarations!
  44.  
  45. Haim Zamir
  46. MacroMind Inc.
  47. D3431
  48.  
  49.